﻿using UnityEngine;
using System.Collections;

public class HeadTowardDirection : MonoBehaviour {

	public float turnSpeed = 3.0f;

	private Vector3 lastPosition;
	// Use this for initialization
	void Start () {
		lastPosition = transform.position;
	}
	
	// Update is called once per frame
	void Update () {
		Vector3 moveDirection = transform.position - lastPosition;
		lastPosition = transform.position;

		moveDirection.z = 0;
		moveDirection.Normalize();
		
		float targetAngle = Mathf.Atan2(moveDirection.y,moveDirection.x) *  Mathf.Rad2Deg + 90.0f;
		float slerpValue = turnSpeed * Time.deltaTime;
		transform.rotation = Quaternion.Slerp(transform.rotation, 
		                                      Quaternion.Euler( 0, 0, targetAngle ), 
		                                      slerpValue );
	}
}
